<?php


namespace App\controllers\v1;


use App\controllers\Base;
use Consts\RedisKey;
use Helper\Output;
use Helper\Mysql;
use Helper\Redis;
use Helper\Reg;
use App\models\FdManager;

class Session extends Base
{

    /**
     * 获取历史消息
     * @param $server
     * @param $fd
     * @param $param
     * @throws \Exception
     */
    public function getHistoryMessge($request, $response){
        $page = $request->post['page']?:1;
        $pagesize = $request->post['page_size']?:20;
        $sessid = $request->post['sessid'];

        $list = Mysql::getInstance()
                ->where('session_id',$sessid)
                ->orderBy('create_at','desc')
                ->get('user_chat_record',[($page-1)*$pagesize,$pagesize]);

        $total = Mysql::getInstance()->where('session_id',$sessid)->getValue('user_chat_record','count(*)');

        return Output::json(200,['list'=> $list ,'total'=> $total]);

    }


    /**
     * 发送消息
     * @param $server
     * @param $fd
     * @param $param
     * @throws \Exception
     */
    public function sendMsg($request, $response){

        $param =  $request->post;
        //检测有没有创建会话关系
        $sessi = Mysql::getInstance()->where('id',$param['sessid'])->where('end_at',0)->getOne("customer_session");
        if(!$sessi){
            return Output::json(1016);
        }
        $sessid = $param['sessid'];
        $data = [
            'send_uid'          => $param['uid'],
            'accept_uid'        => $param['accept_uid'],
            'content'           => $param['content'],
            'type'              => $param['type'],
            'session_id'        => $sessid,
            'create_at'         => time(),
            'send_user_type'    => 2,
        ];

        $res = Mysql::getInstance()->insert('user_chat_record',$data);
        if(!$res){
            return Output::json(1016);
        }
        $data['id'] = Mysql::getInstance()->getInsertId();
        $acceptfd =  FdManager::getFdByAuid($param['accept_uid']);
        //推送给客服
        if(Reg::getInstance()->get('server')->isEstablished($acceptfd)){

            Reg::getInstance()->get('server')->push($acceptfd,Output::json(2000,$data));
        }

        return  Output::json(200);

    }


    /**
     * 上传聊天图片
     * @param $request
     * @param $response
     * @return false|string
     */
    public function uploadImg($request, $response)
    {
        if(!isset($request->files)){

            return Output::json(1028);
        }

        $file_name = parent::uploadImgs($request);
        if(!$file_name){

            return  Output::json(400);
        }
        return  Output::json(200 ,['file_name'=> $file_name ]);

    }


    /**
     * 上传聊天视频
     * @param $request
     * @param $response
     * @return false|string
     */
    public function uploadVideo($request, $response)
    {
        if(!isset($request->files)){

            return Output::json(1028);
        }

        $file_name =  parent::uploadVideos($request);
        if(!$file_name){

            return  Output::json(400);
        }
        return  Output::json(200 ,['file_name'=> $file_name ]);

    }




}